﻿using System;
using DBProvider.Providers;
using DBProvider;
using System.Web.UI.WebControls;

namespace HealthCommunity
{
	public partial class UserInfo : System.Web.UI.Page
	{
		private IUserProvider _UserProvider;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindStateProvince();
				BindUserInfo();
			}
		}

		private void BindUserInfo()
		{
			if (SiteContext.Current.CurrentUser != null)
			{
				var user = SiteContext.Current.CurrentUser;

				txtAddress.Text = user.Address;
				txtEmail.Text = user.Email;
				txtFax.Text = user.Fax;
				txtPhoneNumber.Text = user.PhoneNumber;
				txtQQ.Text = user.QQ;
				txtSFZ.Text = user.ShengFenZheng;
				txtTel.Text = user.Telephone;
				txtUserCName.Text = user.UserCName;
				txtUserName.Text = user.UserName;
				txtPosition.Text = user.Position;
				txtUnitName.Text = user.UnitName;
				foreach (ListItem item in ddlProvince.Items)
				{
					if (item.Value == user.StateProvinceID.ToString())
					{
						item.Selected = true;
						BindCity();
					}
				}

				foreach (ListItem item in ddlCity.Items)
				{
					if (item.Value == user.CityID.ToString())
					{
						item.Selected = true;
					}
				}
				if (user.Sex.Equals(2))
				{
					rblSexFemale.Checked = true;
				}
				else if (user.Sex.Equals(1))
				{
					rblSexMale.Checked = true;
				}
				else if (user.Sex.Equals(0))
				{
					rblSexKeepSecret.Checked = true;
				}

				if (user.Picture == null)
				{
					imgHeader.ImageUrl = "~/template/noimage.jpg";
				}
				else
				{
					imgHeader.ImageUrl = "~/ImageHandler.ashx?picturesize=135&userid=" + user.UserID.ToString();
				}
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			if (IsValid && SiteContext.Current.CurrentUser != null)
			{
				var user = SiteContext.Current.CurrentUser;

				user.Address = txtAddress.Text.Trim();
				if (!string.IsNullOrEmpty(ddlProvince.SelectedValue))
				{
					user.StateProvinceID = int.Parse(ddlProvince.SelectedValue);
				}
				if (!string.IsNullOrEmpty(ddlCity.SelectedValue))
				{
					user.CityID = int.Parse(ddlCity.SelectedValue);
				}
				user.Email = txtEmail.Text.Trim();
				user.Fax = txtFax.Text.Trim();
				user.PhoneNumber = txtPhoneNumber.Text.Trim();
				user.QQ = txtQQ.Text.Trim();
				user.UnitName = txtUnitName.Text.Trim();
				user.Position = txtPosition.Text.Trim();
				if (rblSexFemale.Checked)
				{
					user.Sex = 2;
				}
				else if (rblSexMale.Checked)
				{
					user.Sex = 1;
				}
				else if (rblSexKeepSecret.Checked)
				{
					user.Sex = 0;
				}
				else
				{
					user.Sex = -1;
				}
				user.ShengFenZheng = txtSFZ.Text.Trim();
				user.Telephone = txtTel.Text.Trim();
				user.UserCName = txtUserCName.Text.Trim();
				user.UserName = txtUserName.Text.Trim();
				user.ZipCode = txtZipCode.Text.Trim();

				if (fuHeader.HasFile)
				{
					user.Picture = fuHeader.FileBytes;
				}

				if (_UserProvider == null)
				{
					_UserProvider = new UserProvider();
				}

				if (_UserProvider.UpdateUser(user, false))
				{
					ltrMsg.Text = "保存成功";

					BindUserInfo();
				}
				else
				{
					ltrMsg.Text = "保存失败了";
				}
			}
		}

		protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindCity();
		}

		private void BindCity()
		{
			var selectedItem = ddlProvince.SelectedValue;

			if (!string.IsNullOrEmpty(selectedItem))
			{
				var selectedProvinceID = int.Parse(selectedItem);

				var areaProvider = new AreaProvider();
				var cities = areaProvider.GetCityByStateProvince(selectedProvinceID);

				ddlCity.DataSource = cities;
				ddlCity.DataBind();
			}
		}

		private void BindStateProvince()
		{
			var areaProvider = new AreaProvider();

			ddlProvince.Items.Clear();
			ddlProvince.DataSource = areaProvider.GetAllStateProvince();
			ddlProvince.DataBind();
		}
	}
}